import asyncio

from redis_config import set_progress


async def debug_func(
    task_id=None, error=True, error_phase=1, output_dir="NA", delay=1, **kwargs
):
    print("start")
    if error_phase == 1 and error:
        raise Exception("Raising at 1!")
    await asyncio.sleep(delay)
    set_progress(task_id=task_id, progress=1 / 3)

    if error_phase == 2 and error:
        raise Exception("Raising at 2!")
    await asyncio.sleep(delay)
    set_progress(task_id=task_id, progress=2 / 3)

    if error_phase == 3 and error:
        raise Exception("Raising at 3!")
    await asyncio.sleep(delay)
    set_progress(task_id=task_id, progress=3 / 3)
    if error_phase == 4 and error:
        raise Exception("Raising at 4!")

    import os

    # Ensure the output directory exists
    os.makedirs(output_dir, exist_ok=True)

    # Define the path for the text file
    text_file_path = os.path.join(output_dir, "debug_output.txt")

    # Write a basic text file to the output directory
    with open(text_file_path, "w", encoding="utf-8") as file:
        file.write("This is a basic text file generated by debug_func.\n")
    print("done")
